import PaginationBase from '@/class/Pagination/base'
import { Message } from 'element-ui'
import _ from 'lodash'

/**
 * @desc 目前只写了allChoose：0的情况，对于allChoose：1的逻辑还没写。。。
 */
export default class PaginationSelect extends PaginationBase {
  constructor(scope, option = {}) {
    super(scope, option)
    if (!scope) {
      return
    }
    this.scope = scope
    this.tableScope = scope

    this.rember = {
      toggle: option.isRember || false,
      ref: option.remberRef || 'elTableRef',
      remberId: option.remberId || 'id',
      all: [],
      allChoose: '0',
      removeAll: []
    }
  }

  // 更改作用域方法
  changeTableScope(scope) {
    this.tableScope = scope
  }

  /**
   * @desc 初始化参数
   */
  initRember() {
    this.rember.all = []
    this.rember.removeAll = []
    this.rember.allChoose = '0'
    this.scope[this.keys.table].selectNum = 0
    return this
  }

  /**
   * @desc 处理勾选
   */
  handleSelect() {
    this.updateRember()
  }

  /**
   * @desc 根据现在已经选中的 更新 rember.all  rember.removeAll
   */
  updateRember() {
    const _p = this.tableScope.$refs[this.rember.ref]

    if (!_p) {
      throw new Error('要使用记忆勾选，请设置table的ref属性')
    }

    const data = this.scope[this.keys.table].data
    const selected = _p.selection
    const notSelect = data.filter((item) => {
      return !selected.includes(item)
    })
    if (this.rember.allChoose === '1') {
      // 逻辑没有写完
      notSelect.forEach((item) => {
        if (!this.rember.removeAll.includes(item[this.rember.remberId])) {
          this.rember.removeAll.push(item)
        }
      })
    } else if (this.rember.allChoose === '0') {
      this.rember.all = this.rember.all.filter((item) => {
        return !data.some(
          (item2) => item[this.rember.remberId] === item2[this.rember.remberId]
        )
      })
      this.rember.all = [...this.rember.all, ...selected]

      this.scope[this.keys.table].selectNum = this.rember.all.length
    }
    return this
  }

  /**
   * @desc 当列表页数据更新时。根据rember.all  rember.removeAll  去更改选中项
   * @returns 无
   */
  dealRember() {
    const rembers = this.rember
    const data = this.scope[this.keys.table].data

    if (!rembers.toggle) {
      this.initRember()
      return
    }

    this.tableScope.$nextTick(() => {
      const self = this
      if (rembers.allChoose === '0') {
        data.forEach((item) => {
          rembers.all.forEach((item2) => {
            if (item2[rembers.remberId] === item[rembers.remberId]) {
              self.tableScope.$refs[self.rember.ref].toggleRowSelection(item, true)
            }
          })
        })
        this.scope[this.keys.table].selectNum = rembers.all.length
      } else if (rembers.allChoose === '1') {
        // 逻辑没有写完
      }
    })
    return this
  }

  /**
   * @desc 获取勾选
   */
  getRember(options) {
    const rembers = this.rember
    let isAutoId = true
    if (options && options.isNotAutoId) {
      isAutoId = false
    }
    options = Object.assign(
      {
        chooseKey: 'allChoose',
        allKey: 'ids',
        removeAllKey: 'removeIds',
        mapKey: this.rember.remberId
      },
      options
    )

    if (rembers.allChoose === '0') {
      return {
        [options.chooseKey]: rembers.allChoose,
        [options.allKey]: isAutoId ? rembers.all.map((item) => item[options.mapKey]) : _.cloneDeep(rembers.all)
      }
    } else if (rembers.allChoose === '1') {
      return {
        [options.chooseKey]: rembers.allChoose,
        [options.removeAllKey]: isAutoId ? rembers.removeAll.map(
          (item) => item[options.mapKey]
        ) : _.cloneDeep(rembers.removeAll)
      }
    }
  }

  /**
   * @desc 检查勾选
   */
  checkRember(options) {
    options = Object.assign(
      {
        success: (selection) => {
          console.log(selection)
        }
      },
      options
    )

    const rembers = this.rember
    if (rembers.allChoose === '0') {
      if (rembers.all.length < 1) {
        Message({
          message: '你还没勾选任何项目！',
          type: 'error',
          duration: 5 * 1000
        })
      } else {
        options.success(this.getRember(options))
      }
    } else if (rembers.allChoose === '1') {
      if (this.removeAll.length >= this.pageOption.total) {
        Message({
          message: '你还没勾选任何项目！',
          type: 'error',
          duration: 5 * 1000
        })
      } else {
        options.success(this.getRember(options))
      }
    }
    return this
  }

  /**
   * @desc 清除已经选中的
   * @returns 无
   */
  clearRember(val) {
    let isClearAll = true
    const rembers = this.rember
    const selection = this.tableScope.$refs[this.rember.ref]
    if (selection) selection.clearSelection()

    if (_.isNumber(val) || _.isString(val)) {
      isClearAll = false
      if (rembers.allChoose === '0') {
        rembers.all = rembers.all.filter(item => {
          return item[rembers.remberId] !== val
        })
      }
    } else if (_.isObject(val)) {
      isClearAll = false
      if (rembers.allChoose === '0') {
        rembers.all = rembers.all.filter(item => {
          return item[rembers.remberId] !== val[rembers.remberId]
        })
      }
    }

    if (isClearAll) {
      this.initRember()
    } else {
      this.scope[this.keys.table].selectNum = this.rember.all.length
      this.dealRember()
    }
    return this
  }

  /**
   * @desc 用于非记忆模式下，根据rember.all 进行项目勾选
   * @returns 无
   */
  selectByNotRember() {
    const rembers = this.rember
    const data = this.scope[this.keys.table].data

    this.tableScope.$nextTick(() => {
      const self = this
      if (rembers.allChoose === '0') {
        this.scope[this.keys.table].selectNum = this.rember.all.length
        data.forEach((item) => {
          const f = rembers.all.some(item2 => {
            return item2[rembers.remberId] === item[rembers.remberId]
          })
          if (f) {
            self.tableScope.$refs[self.rember.ref].toggleRowSelection(item, true)
          } else {
            self.tableScope.$refs[self.rember.ref].toggleRowSelection(item, false)
          }
        })
      } else if (rembers.allChoose === '1') {
        // 逻辑没有写完
      }
    })
    return this
  }
}
